<?php

namespace app\api\controller;

use think\Controller;
use think\Request;
use app\api\model\PurchaseModel;
use app\api\validate\PurchaseValidate;
use think\Db;
use app\http\TokenTrait;
use think\facade\Cache;
class Purchase extends Common
{
    use TokenTrait;
    /**
     * 采购订单 列表
     * @return \think\response\Json
     */
    public function PurchaseList(Request $request, PurchaseModel $model)
    {
        $data =$request->param();
        $data['start_time'] =isset($data['start_time']) ? strtotime($data['start_time']) : '';
        $data['end_time'] =isset($data['end_time']) ? strtotime($data['end_time']) : '';
        $list = $model->getPurchaseList($data);
        if (!empty($list)) {
            return json(['code' => 200, 'msg' => '成功', 'list' => $list]);
        } else {
            return json(['code' => 404, 'msg' => '失败']);
        }
    }

    /*public function getSpec(Request $request)
    {
        $data =$request->param();
        $data['maker'] =self::getUId();
        $key =self::getUId();
        switch ($data['type']){
            case "采购单":
                Cache::set($key.'-'.'purchase',$data,1800);
                $key = $key.'-'.'purchase';
                break;
            case "采购退货单":
                Cache::set($key.'-'.'purchase_return',$data,1800);
                $key =$key.'-'.'purchase_return';
                break;
            case "销售单":
                Cache::set($key.'-'.'sale',$data,1800);
                $key =$key.'-'.'sale';
                break;
            case "销售退货单":
                Cache::set($key.'-'.'sale_return',$data,1800);
                $key =$key.'-'.'sale_return';
                break;
            case "配货单":
                Cache::set($key.'-'.'prepare',$data,1800);
                $key =$key.'-'.'prepare';
                break;
            case "配货退货单":
                Cache::set($key.'-'.'prepare_return',$data,1800);
                $key =$key.'-'.'prepare_return';
                break;
            case "调拨单":
                Cache::set($key.'-'.'allot',$data,1800);
                $key =$key.'-'.'allot';
                break;
            default:
                Cache::set($key.'-'.'production',$data,1800);
                $key =$key.'-'.'production';
        }
        return $this->returnJson(200,'成功',$key);
    }

    public function getCacheSpec(Request $request)
    {
        $data =$request->param('key');
        $list ='';
        switch ($data) {
            case self::getUId().'-purchase':
                $list =Cache::get(self::getUId().'-purchase');
                break;
            case self::getUId().'-purchase_return':
                $list =Cache::get(self::getUId().'-purchase_return');
                break;
            case self::getUId().'-sale':
                $list =Cache::get(self::getUId().'-sale');
                break;
            case self::getUId().'-sale_return':
                $list =Cache::get(self::getUId().'-sale_return');
                break;
            case self::getUId().'-prepare':
                $list =Cache::get(self::getUId().'-prepare');
                break;
            case self::getUId().'-prepare_return':
                $list =Cache::get(self::getUId().'-prepare_return');
                break;
            case self::getUId().'-allot':
                $list =Cache::get(self::getUId().'-allot');
                break;
            default:
                $list =Cache::get(self::getUId().'-production');
        }
        $list['option'] =json_decode($list['option'],true);
        return $this->returnJson(200,'成功',$list);
    }

    public function getCacheColor($id , PurchaseModel $model)
    {
        $list =$model->getCacheColor($id);
        return $this->returnJson(200,'成功',$list);
    }
    public function getCacheSize($id , PurchaseModel $model)
    {
        $list =$model->getCacheSize($id);
        return $this->returnJson(200,'成功',$list);
    }

    public function getEditProduct(Request $request , PurchaseModel $model)
    {
        $data =json_decode($request->param('data'),true);
        $key =$request->param('key');
        switch ($key) {
            case self::getUId().'-purchase':
                $spec =Cache::get(self::getUId().'-purchase');
                break;
            case self::getUId().'-purchase_return':
                $spec =Cache::get(self::getUId().'-purchase_return');
                break;
            case self::getUId().'-sale':
                $spec =Cache::get(self::getUId().'-sale');
                break;
            case self::getUId().'-sale_return':
                $spec =Cache::get(self::getUId().'-sale_return');
                break;
            case self::getUId().'-prepare':
                $spec =Cache::get(self::getUId().'-prepare');
                break;
            case self::getUId().'-prepare_return':
                $spec =Cache::get(self::getUId().'-prepare_return');
                break;
            case self::getUId().'-allot':
                $spec =Cache::get(self::getUId().'-allot');
                break;
            default:
                $spec =Cache::get(self::getUId().'-production');
        }
        $spec['option'] =json_decode($spec['option'],true);
        $list =$model->getEditProduct($data,$spec);
        return $this->returnJson(200,'成功',$list);
    }*/

    public function getGoodsStock(Request $request , PurchaseModel $model)
    {
        $data =$request->param();
        $list =$model->getGoodsStock($data);
        return $this->returnJson(200,'成功',$list);
    }

    /**
     * 采购订单 新增
     * @return \think\response\Json
     */
    public function PurchaseAdd(Request $request, PurchaseModel $model)
    {
        $data = $request->post();
        $data['number'] = 'jxc' . time();//单号
        $data['uid'] = self::getUId();//制单人id
        $data['create_time'] =strtotime($data['create_time']);
        $data['type1'] =1;//1采购单
        $data['images'] =json_encode($data['images']);
        $validate = new PurchaseValidate();
        if (!$validate->check($data)) {
            return json(['code' => 404, 'msg' => $validate->getError()]);
        }
        $arr = $model->getPurchaseAdd($data);
        if (!empty($arr)) {
            return json(['code' => 200, 'msg' => '添加成功','list'=>$data]);
        } else {
            return json(['code' => 500, 'msg' => '添加失败','list'=>$data]);
        }
    }


    public function PurchaseDraft($id,Request $request,PurchaseModel $model)
    {
        $data =$request->post();
        $data['number'] = 'jxc' . time();//单号
        $data['uid'] = self::getUId();//制单人id
        $data['create_time'] =strtotime($data['create_time']);
        $data['type1'] =1;//1采购单
        $data['status'] =5;
        $data['images'] =json_encode($data['images']);
        $list = $model->getPurchaseDraft($id,$data);
        if (!empty($list)) {
            return $this->returnJson(200,'ok',$list);
        } else {
            return json(['code' => 404, 'msg' => '失败']);
        }
    }

    public function getPutPurchase($id,Request $request,PurchaseModel $model)
    {
        try{
            $data =$request->post();
            $data['number'] = 'jxc' . time();//单号
            $data['uid'] = self::getUId();//制单人id
            $data['create_time'] =strtotime($data['create_time']);
            $data['type'] =1;//1采购单
            $data['status'] =1;
            $data['images'] =json_encode($data['images']);
            $arr = $model->getPutStock($id,$data);
            if($arr){
                return $this->returnJson(200,  'ok');
            }else{
                return $this->returnJson(500,  $arr);
            }
        } catch (\Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage()], 500);
        }
    }

    /**
     * 采购订单  ------详情
     * @param $id 采购订单id
     * @return \think\response\Json
     */
    public function PurchaseDetail($id,PurchaseModel $model)
    {
        $list = $model->getPurchaseInfo($id);
        if (!empty($list)) {
            return json(['code' => 200, 'msg' => '成功', 'list' => $list]);
        } else {
            return json(['code' => 404, 'msg' => '失败']);
        }
    }

    /**
     * 采购订单 删除
     * @return string|\think\response\Json
     */
    public function PurchaseDel($id,PurchaseModel $model)
    {
        try {
            $list = $model->getPurchaseDel($id);
            if (is_string($list)) {
                return $this->returnJson(500,$list);
            } else {
                return $this->returnJson(200,'ok');
            }
        } catch (\Exception $e) {
            return $this->returnJson(500,$e->getMessage());
        }
    }

    /**
     * 采购订单 撤销
     * @return string|\think\response\Json
     */
    public function PurchaseRevoke($id,PurchaseModel $model)
    {
        try {
            $list = $model->getPurchaseRevoke($id);
            if (is_string($list)) {
                return $this->returnJson(500,$list);
            } else {
                return $this->returnJson(200,'ok');
            }
        } catch (\Exception $e) {
            return $this->returnJson(500,$e->getMessage());
        }
    }

    /**
     * 采购订单 部分入库
     */
    public function Purchasedepot($id,Request $request,PurchaseModel $model)
    {
        $option =json_decode($request->param('option'), true);
        try {
            $list = $model->getDepot($id, $option);
            if ($list == 1) {
                return json(['code' => 200, 'msg' => '成功']);
            } else {
                return json(['code' => 500, 'msg' => '输入数量超出库存上限']);
            }
        } catch (\Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage()], 500);
        }
    }

    /**
     * 采购订单 全部入库
     * @return string|\think\response\Json
     */
    public function alldepot($id,PurchaseModel $model)
    {
        $list = $model->getAllDepot($id);
        if (!empty($list)) {
            return json(['code' => 200, 'msg' => '成功']);
        } else {
            return json(['code' => 404, 'msg' => '失败']);
        }
    }

    /**
     * 采购退货  列表
     * @return string|\think\response\Json
     */
    public function returnList(Request $request, PurchaseModel $model)
    {
        $data =$request->param();
        $data['start_time'] =isset($data['start_time']) ? strtotime($data['start_time']) : '';
        $data['end_time'] =isset($data['end_time']) ? strtotime($data['end_time']) : '';
        $list = $model->getReturnList($data);
        if (!empty($list)) {
            return json(['code' => 200, 'msg' => '成功', 'list' => $list]);
        } else {
            return json(['code' => 404, 'msg' => '失败']);
        }
    }

    /**
     * 采购退货  新增
     * @return \think\response\Json
     */
    public function returnAdd(Request $request,PurchaseModel $model)
    {
        $data =$request->param();
        $data['uid'] = self::getUId();
        $data['create_time'] =strtotime($data['create_time']);
        $data['number'] = 'jxc' . time();
        $data['type'] =4;
        $validate = new PurchaseValidate();
        if (!$validate->check($data)) {
            return json(['code' => 404, 'msg' => $validate->getError()]);
        }
        $list = $model->getReturnAdd($data);
        if (!empty($list)) {
            return json(['code' => 200, 'msg' => '成功']);
        } else {
            return json(['code' => 404, 'msg' => '失败']);
        }
    }

    public function purchaseReturnDraft($id,Request $request,PurchaseModel $model)
    {
        $data =$request->post();
        $data['number'] = 'jxc' . time();//单号
        $data['uid'] = self::getUId();//制单人id
        $data['create_time'] =strtotime($data['create_time']);
        $data['type'] =4;//4采购退货单
        $data['status'] =3;
        $list = $model->getReturnDraft($id,$data);
        if (!empty($list)) {
            return $this->returnJson(200,'ok',$list);
        } else {
            return json(['code' => 404, 'msg' => '失败']);
        }
    }

    public function purchaseReturn($id,Request $request,PurchaseModel $model)
    {
        try{
            $data =$request->post();
            $data['uid'] = self::getUId();//制单人id
            $data['create_time'] =strtotime($data['create_time']);
            $data['type'] =4;//4采购退货单
            $data['status'] =1;
            $arr = $model->getPurchaseReturn($id,$data);
            if($arr){
                return $this->returnJson(200,  'ok');
            }else{
                return $this->returnJson(200,  $arr);
            }
        } catch (\Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage()], 500);
        }
    }

    /**
     * 采购退货  ------详情
     * @param $id 采购退货id
     * @return \think\response\Json
     */
    public function returnDetail($id,PurchaseModel $model)
    {
        $list = $model->getReturnDetail($id);
        if (!empty($list)) {
            return json(['code' => 200, 'msg' => '成功', 'list' => $list]);
        } else {
            return json(['code' => 404, 'msg' => '失败']);
        }
    }

    /*public function returnDepot($id,Request $request,PurchaseModel $model)
    {
        $option =$request->param('option');
        try {
            $list = $model->getReturnDepot($id, $option);
            if ($list == 1) {
                return json(['code' => 200, 'msg' => '成功']);
            } else {
                return json(['code' => 500, 'msg' => '输入数量超出库存上限']);
            }
        } catch (\Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage()], 500);
        }
    }*/

    /**
     * 采购退货  ------撤销
     * @param $id 采购退货单id
     * @return \think\response\Json
     */
    public function returnRevoke($id,PurchaseModel $model)
    {
        $list = $model->getReturnRevoke($id);
        if ($list ==1) {
            return json(['code' => 500, 'msg' => '单据无法撤销']);
        } else {
            return json(['code' => 200, 'msg' => '成功']);
        }
    }

    /**
     * 采购退货  ------删除
     * @param $id 采购退货单id
     * @return \think\response\Json
     */
    public function returnDel($id,PurchaseModel $model)
    {
        $list = $model->getReturnDel($id);
        if ($list == 1) {
            return json(['code' => 500, 'msg' => '单据无法删除']);
        } else {
            return json(['code' => 200, 'msg' => '成功']);
        }
    }

    public function getTodayNum(Request $request,PurchaseModel $model)
    {
        $list = $model->getTodayNum();
        if (!empty($list)) {
            return $this->returnJson(200,'success',$list);
        } else {
            return $this->returnJson(500,'fail');
        }
    }
}